/**
 *Copyright(c) HNA SYSTEMS Co., LTD
 *
 *@date 2013-7-23
 * 	
 *Original Author: 郑兴(zhengxing)
 *
 *ChangeLog
 *
 */
package com.hnas.core.mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;

//import org.apache.commons.beanutils.BeanUtils;

/**
 * @author 郑兴(zhengxing)
 *
 */
public class Mapper<V, E> {

	private Class<V> valueClass;
	private Class<E> entityClass;
	
	/**
	 * @deprecated Use Mapper(Class<V> theValue, Class<E> theEntity) instead
	 */
	protected Mapper() {}
	
	protected Mapper(Class<V> theValue, Class<E> theEntity) {
		this.valueClass = theValue;
		this.entityClass = theEntity;
	}
	
	/**
	 * <pre>获取转换实例</pre>
	 * @Auther 郑兴(zhengxing)
	 * @Date 2013-7-23
	 * @param <V>
	 * @param <E>
	 * @param valueClass
	 * @param entityClass
	 * @return
	 */
	public static <V, E> Mapper<V, E> getInstance(Class<V> valueClass, Class<E> entityClass) {
		Mapper<V, E> m = new Mapper<V, E>(valueClass, entityClass);
		return m;
	}
	
	/**
	 * <pre>实体转换成DTO</pre>
	 * @Auther 郑兴(zhengxing)
	 * @Date 2013-7-23
	 * @param entity
	 * @return
	 */
	public V mapToValue(E entity) {
		V value = null;
		if (null==entity)
			return value;

		try {
			value = getValueClass().newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException("Error creating new instance of : " + getValueClass().getName(), e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException("Error creating new instance of : " + getValueClass().getName(), e);
		}

		try {
			BeanUtils.copyProperties(entity,value );
		
		} catch (Exception e) {
			throw new RuntimeException("Error copying properties of : " + entity.getClass().getName(), e);
		}

		return value;
	}

	/**
	 * <pre>DTO转换成实体</pre>
	 * @Auther 郑兴(zhengxing)
	 * @Date 2013-7-23
	 * @param value
	 * @return
	 */
	public E mapToEntity(V value) {
		E entity = null;

		try {
			//entity = entityClass.newInstance();
			entity = getEntityClass().newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException("Error creating new instance of : " + getEntityClass().getName(), e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException("Error creating new instance of : " + getEntityClass().getName(), e);
		}

		try {
			BeanUtils.copyProperties(value,entity);
		}  catch (Exception e) {
			throw new RuntimeException("Error copying properties of : " + value.getClass().getName(), e);
		}

		return entity;
	}

	/**
	 * <pre>实体集合转换成DTO集合</pre>
	 * @Auther 郑兴(zhengxing)
	 * @Date 2013-7-23
	 * @param entityList
	 * @return
	 */
	public List<V> mapToValues(List<E> entityList) {
		if (entityList != null) {
			List<V> valueList = new ArrayList<V>();
			for (E entity : entityList) {
				valueList.add(mapToValue(entity));
			}
			return valueList;
		} else {
			return null;
		}
	}
	
	/**
	 * <pre>实体集合转换成DTO集合</pre>
	 * @Auther 郑兴(zhengxing)
	 * @Date 2013-7-23
	 * @param entitySet
	 * @return
	 */
	public List<V> mapToValues(Set<E> entitySet) {
		if (entitySet != null) {
			List<V> valueList = new ArrayList<V>();
			for (E entity : entitySet) {
				valueList.add(mapToValue(entity));
			}
			return valueList;
		} else {
			return null;
		}
	}

	/**
	 * <pre>DTO集合转换成实体集合</pre>
	 * @Auther 郑兴(zhengxing)
	 * @Date 2013-7-23
	 * @param valueList
	 * @return
	 */
	public List<E> mapToEntities(List<V> valueList) {
		if (valueList != null) {
			List<E> entityList = new ArrayList<E>();
			for (V value : valueList) {
				entityList.add(mapToEntity(value));
			}
			return entityList;
		} else {
			return null;
		}
	}
	
	/**
	 * @return
	 */
	protected Class<E> getEntityClass() {
		return entityClass;
	};

	/**
	 * @return
	 */
	protected Class<V> getValueClass() {
		return valueClass;
	};
}
